struct BinaryNode
{
    int val;
    BinaryNode left;
    BinaryNode right;
}

int SumTree(BinaryNode root){
	if(root == null)
    {
	 	return 0;
    }
    else
    {
        int result = root.val + SumTree(root.left) + SumTree(root.right);
	    return result;
    }
}

void addValue(BinaryNode root, int value)
{
    if(value > root.val)
    {
        if(root.right == null)
        {
            BinaryNode right;
            right.val = value;
            root.right = right;
        }
        else
        {
            addValue(root.right, value);
        }
    }
    else
    {
        if(root.left == null)
        {
            BinaryNode left;
            left.val = value;
            root.left = left;
        }
        else
        {
            addValue(root.left, value);
        }
    }
}

void preOrderPrint(BinaryNode root)
{
    if(root == null)
    {
        
    }
    else
    {
        println(root.val);
        preOrderPrint(root.left);
        preOrderPrint(root.right);
    }
}

void inOrderPrint(BinaryNode root)
{
    if(root == null)
    {
        
    }
    else
    {        
        inOrderPrint(root.left);
        println(root.val);
        inOrderPrint(root.right);
    }
}

void postOrderPrint(BinaryNode root)
{
    if(root == null)
    {
        
    }
    else
    {        
        postOrderPrint(root.left);
        postOrderPrint(root.right);
        println(root.val);
    }
}

int main()
{
    BinaryNode root;    
    root.val = 1;

    addValue(root, 3);
    addValue(root, 5);
    addValue(root, 2);
    addValue(root, 4);

    println("The sum is: ");
    println(SumTree(root));

    println("preOrder:");
    preOrderPrint(root);

    println();
    println("postOrder");
    postOrderPrint(root);

    println();
    println("inOrder:");
    inOrderPrint(root);

}
